package com.example.projectcode.controller.Student;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.projectcode.model.*;
import com.example.projectcode.result.Result;
import com.example.projectcode.service.*;
import com.example.projectcode.vo.CpstuanVo;
import com.example.projectcode.vo.CstuanVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(tags = "题目练习接口")
@RestController
@RequestMapping(value = "/user/practiceQuestion")
public class QuestionPracticeController {
    @Autowired
    private SubjectService subjectService;

    @Autowired
    private ChapterService chapterService;

    @Autowired
    private TestPointService testPointService;

    @Autowired
    private ChoiceService choiceService;

    @Autowired
    private ComprehensiveService comprehensiveService;

    @Autowired
    private CstuanService cstuanService;

    @Autowired
    private CrecordService crecordService;

    @Autowired
    private CpstuanService cpstuanService;

    @Autowired
    private CprecordService cprecordService;

    @ApiOperation(value = "显示所有科目")
    @GetMapping("/queryAllSubjects")
    public Result queryAllSubjects() {
        List<Subject> subjectList = subjectService.selectAllSubject();
        return Result.ok(subjectList);
    }

    @ApiOperation(value = "根据科目id查询对应的章节")
    @GetMapping(value = "/queryChapterBySubid/{Subid}")
    public Result queryChapterBySubid(@PathVariable Integer Subid) {
        List<Chapter> chapterList = chapterService.selectAllChapterBySubid(Subid);
        return Result.ok(chapterList);
    }

    @ApiOperation(value = "根据章节关键字进行模糊查询")
    @GetMapping(value = "/queryChapterByKeyWord/{KeyWord}")
    public Result queryChapterByKeyWord(@PathVariable String KeyWord) {
        List<Chapter> chapterList = chapterService.selectChapterByKeyWord(KeyWord);
        return Result.ok(chapterList);
    }


    @ApiOperation(value = "根据章节id查询对应的考点")
    @GetMapping(value = "/queryTestPointByChapid/{Chapid}")
    public Result queryTestPointByChapid(@PathVariable Integer Chapid) {
        List<Testpoint> testPointList = testPointService.selectAllTestPointByChapid(Chapid);

        return Result.ok(testPointList);
    }


    @ApiOperation(value = "根据考点id,题类别，题型查询题库")
    @GetMapping(value = "/queryQuestion/{TestPointid}/{questionCategory}/{questionType}")
    public Result queryAllQuestion(@PathVariable Integer TestPointid,
                                   @PathVariable String questionCategory,
                                   @PathVariable String questionType) {
        List<Choice> choiceList = new ArrayList<>();
        List<Comprehensive> comprehensiveList = new ArrayList<>();


        if (questionType.equals("选择题")) {
            choiceList = choiceService.selectAllChoice(TestPointid, questionCategory);
        }
        if (questionType.equals("综合题")) {
            comprehensiveList = comprehensiveService.selectAllComprehensive(TestPointid, questionCategory);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("choiceList", choiceList);
        map.put("comprehensiveList", comprehensiveList);
        return Result.ok(map);

    }

    @ApiOperation(value = "根据题号选择题目")
    @GetMapping(value = "/getQuestionByOrder/{TestPointid}/{questionCategory}/{questionType}/{order}")
    public Result getQuestionByOrder(@PathVariable Integer TestPointid,
                                     @PathVariable String questionCategory,
                                     @PathVariable String questionType,
                                     @PathVariable Integer order
    ) {
        Choice choice = new Choice();
        Comprehensive comprehensive = new Comprehensive();
        int choiceCount;
        int comprehensiveCount;


        if (questionType.equals("选择题")) {
            Map<String,Object> map = new HashMap<>();
            choice = choiceService.selectChoice(TestPointid, questionCategory, order);
            choiceCount = choiceService.countChoice(TestPointid, questionCategory);
            map.put("选择题题目信息",choice);
            map.put("选择题题目总数",choiceCount);
            map.put("type",questionType);
            return Result.ok(map);
        }

        if (questionType.equals("综合题")) {
            Map<String,Object> map = new HashMap<>();
            comprehensive = comprehensiveService.selectComprehensive(TestPointid, questionCategory, order);
            comprehensiveCount = comprehensiveService.countComprehensive(TestPointid, questionCategory);
            map.put("综合题题目信息",comprehensive);
            map.put("综合题题目总数",comprehensiveCount);
            map.put("type",questionType);
            return Result.ok(map);
        }
        return Result.fail().message("获取题库失败");
    }

    @ApiOperation("学生选择题答案保存接口")
    @PostMapping("/saveChoice")
    public Result saveChoice(@RequestBody CstuanVo cstuanVo){
        Cstuan cstuan = new Cstuan();
        cstuan.setCId(cstuanVo.getC_id());
        cstuan.setStuId(cstuanVo.getStu_id());
        cstuan.setCStuanc(cstuanVo.getC_stuanc());

        if (cstuanVo.getCanswer().equals(cstuanVo.getC_stuanc())){
            //学生作答正确
            cstuan.setCFeedback("true");
        }else {
            cstuan.setCFeedback("false");
        }
        //保存学生作答的答案
        //回填主键id
        int result = cstuanService.insertCstuan(cstuan);

        //保存到记录表
        Crecord crecord = new Crecord();
        crecord.setCTime(new Date().toString());
        crecord.setCStuanid(cstuan.getCstuanid());
        crecord.setCId(cstuan.getCId());
        crecord.setStuId(cstuan.getStuId());
        int recordResult = crecordService.insertCrecord(crecord);

        Map<String,Object> map = new HashMap<>(); //包含学生作答情况，正确答案


        if (result>0 && recordResult>0){
            //返回学生作答结果，包含学生答题正确还是错误
            map.put("正确答案",cstuanVo.getCanswer());
            map.put("作答信息",cstuan);
            return Result.ok(map).message("保存答案成功");
        }else {
            return Result.fail().message("保存答案失败");
        }

    }

    @ApiOperation("学生综合题题答案保存接口")
    @PostMapping("/savecomprehensive")
    public Result saveComprehensive(@RequestBody CpstuanVo cpstuanVo){

        Cpstuan cpstuan = new Cpstuan();
        cpstuan.setCpId(cpstuanVo.getCp_id());
        cpstuan.setStuId(cpstuanVo.getStu_id());
        cpstuan.setCpStuanc(cpstuanVo.getCp_stuanc());

        //保存综合题答题答案
        //主键id回填
        //等待老师评语
        int result = cpstuanService.insertCpstuan(cpstuan);

        Cprecord cprecord = new Cprecord();
        cprecord.setCpStuanid(cpstuan.getCpStuanid());
        cprecord.setCpId(cpstuan.getCpId());
        cprecord.setStuId(cpstuan.getStuId());
        cprecord.setCpTime(new Date().toString());

        int recordResult = cprecordService.insertCprecord(cprecord);

        Map<String,Object> map = new HashMap<>(); //包含学生作答情况，正确答案

        if (result > 0 && recordResult >0){
            map.put("正确答案",cpstuanVo.getCpAnswer());
            map.put("作答信息",cpstuan);
            return Result.ok(map).message("答案保存成功");
        }else {
            return Result.fail().message("答案保存失败");
        }



    }


    @ApiOperation("将综合题加入错题记录")
    @GetMapping("/addWrongComprehensive/{CpStuanid}")
    public Result addWrongComprehensive(@PathVariable Integer CpStuanid){
        QueryWrapper<Cpstuan>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Cp_stuanid",CpStuanid);
        boolean result = cpstuanService.update(queryWrapper);
        if (result){
            return Result.ok().message("添加错题成功");
        }else {
            return Result.fail().message("添加错题失败");
        }

    }



    /*------------------------------新加--------------------------------------*/

    @ApiOperation("分页查询所有练习选择题")
    @GetMapping("/queryAllChoice/{stuId}{page}/{size}")
    public Result queryAllChoice(@PathVariable long page, @PathVariable long size){
        Page<Choice> choicePage = new Page<>(page,size);

        IPage<Choice> choiceIPage = choiceService.page(choicePage,null);
        return Result.ok(choiceIPage);
    }

    @ApiOperation("分页查询所有练习综合题")
    @GetMapping("/queryAllComprehensive/{page}/{size}")
    public Result queryAllComprehensive(@PathVariable long page, @PathVariable long size){
        Page<Comprehensive> page1 = new Page<>(page,size);
        IPage<Comprehensive> comprehensiveIPage = comprehensiveService.page(page1,null);

        return Result.ok(comprehensiveIPage);
    }






}
